﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace student_systemApp
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        //添加学生
        private void Menu_addStudent_Click(object sender, EventArgs e)
        {
            //AddStudentForm addStudentForm = AddStudentForm.Instance;
            AddStudentForm addStudentForm = new AddStudentForm();
            addStudentForm.MdiParent = this;
            //addStudentForm.Show();
            oppenMdiForm(addStudentForm);
            pullMdiForm(addStudentForm);
        }
        //显示学生列表
        private void Menu_listStudent_Click(object sender, EventArgs e)
        {
            //ListStudentForm listStudentForm = ListStudentForm.instance;
            ListStudentForm listStudentForm = new ListStudentForm();
            listStudentForm.MdiParent = this;
            //listStudentForm.Show();
            oppenMdiForm(listStudentForm);
            pullMdiForm(listStudentForm);
        }

        //显示年级列表
        private void Menu_listGrade_Click(object sender, EventArgs e)
        {
            ListGradeForm listGradeForm = new ListGradeForm();
            listGradeForm.MdiParent = this;
            //listGradeForm.Show();
            oppenMdiForm(listGradeForm);
            pullMdiForm(listGradeForm);
        }
        //显示班级列表
        private void Memu_listClass_Click(object sender, EventArgs e)
        {
            ListClassForm listClassForm = new ListClassForm();
            listClassForm.MdiParent = this;
            //listClassForm.Show();
            oppenMdiForm(listClassForm);
            pullMdiForm(listClassForm);
        }


        //新增班级
        private void Menu_addClass_Click(object sender, EventArgs e)
        {
            AddClassForm addClassForm = new AddClassForm();
            addClassForm.MdiParent = this;
            oppenMdiForm(addClassForm);
            pullMdiForm(addClassForm);
        }



        //退出程序
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否退出程序", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
            else
            {
                Application.ExitThread();
            }
        }
        //退出
        private void Menu_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {

        }


        /**
         * 打开MDI子窗体,子窗体只能显示一个
         * @param frm
         */
        private void oppenMdiForm(Form frm)
        {
            //判断是否第一次显示窗体
            if (this.MdiChildren.Length == 1)
            {
                frm.MdiParent = this;
                frm.Show();
                return;
            }
            foreach (Form children in this.MdiChildren)
            {
                if (frm == children)
                {
                    //如果是现在就存在的子窗体就激活它
                    children.Activate();
                }
                else
                {
                    children.Close();
                    if (frm == null || frm.IsDisposed)
                    {
                        Type type = frm.GetType();
                        frm = (Form)System.Activator.CreateInstance(type);
                        frm.MdiParent = this;
                        frm.Show();
                    }
                    else
                    {
                        frm.MdiParent = this;
                        frm.Show();
                    }
                }
            }
        }

        /**
         * 子页面铺满整个父页面mdi区域
         */
        private void pullMdiForm(Form form)
        {
            form.WindowState = FormWindowState.Normal;

            form.MdiParent = this;

            form.MaximizeBox = false;

            form.Dock = DockStyle.Fill;
        }

    }
}
